﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace BRS.BidModule
{
    public partial class UserPassword : Form
    {
        Entities brs = new Entities();
        Users user = new Users();
        public UserPassword()
        {
            InitializeComponent();
        }
        public UserPassword(Users users)
        {
            InitializeComponent();
            user = users;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text) && !string.IsNullOrEmpty(textBox3.Text))
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] result = Encoding.Default.GetBytes(this.textBox1.Text.Trim());    //tbPass为输入密码的文本框
                byte[] output = md5.ComputeHash(result);
                string MD5password = BitConverter.ToString(output).Replace("-", "");  //tbMd5pass为输出加密文本的文本框
                if (MD5password == user.Password)
                {
                    if (textBox2.Text == textBox3.Text)
                    {
                        result = null;
                        result = Encoding.Default.GetBytes(this.textBox2.Text.Trim());    //tbPass为输入密码的文本框
                        output = md5.ComputeHash(result);
                        MD5password = BitConverter.ToString(output).Replace("-", "");  //tbMd5pass为输出加密文本的文本框
                        var use = brs.Users.Find(user.UserId);
                        use.Password = MD5password;
                        if (brs.SaveChanges() > 0) 
                        {
                            MessageBox.Show("密码修改成功，请重新登录！");
                            Application.Restart();
                        }
                            
                    } MessageBox.Show("新密码两次不一致！");
                }
                else MessageBox.Show("原密码错误！");

            }
        }
    }
}
